package com.csdj.t301.cloud;

import com.csdj.t301.cloud.annotation.Role;
import com.csdj.t301.cloud.annotation.Validator;
import com.csdj.t301.cloud.entity.Person;
import com.csdj.t301.cloud.entity.Student;

import java.lang.reflect.Field;

/**
 * author:cloud
 * date:2021/1/22 0022
 **/
public class Charactor02 {
    public static void main(String[] args) throws IllegalAccessException {
        Student student = new Student();
        student.setName("kitty");
        student.setAge(200);
        Person person = new Person();
        person.setName("cloud");
        person.setAge(200);
        check(person);
    }

    public static boolean check(Object student) throws IllegalAccessException {
        Class clazz = student.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for(Field field : fields){
            Validator validator =  field.getAnnotation(Validator.class);
            if(validator == null)continue;
//            if(!"int".equalsIgnoreCase(field.getType().toString()))continue;
            field.setAccessible(true);
            Object value = field.get(student);
            if(field.getInt(student) < validator.min()){
                System.out.println(field.getName()+"不能小于最小值");
                return false;
            }
            if(field.getInt(student) > validator.max()){
                System.out.println(field.getName()+"不能大于最大值");
                return false;
            }
        }
        return true;
    }
}
